//
//  WQInputView.m
//  SomeUIKit
//
//  Created by WangQiang on 2016/11/23.
//  Copyright © 2016年 WangQiang. All rights reserved.
//

#import "WQInputView.h"
@interface WQInputView()
@property (strong ,nonatomic) WQImageTextView *textView;
//用于存储辅助视图
@property (weak ,nonatomic) UIView *textInputView;

@property (assign ,nonatomic) CGFloat bottomHeight;//底部的距离(暂时认为是距离键盘的距离)
@end
@implementation WQInputView
-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        _textView = [[WQImageTextView alloc] initWithFrame:self.bounds];
        _textView.backgroundColor = [UIColor redColor];
        _textView.font = MYFont(20.0);
        _textView.layoutManager.allowsNonContiguousLayout = YES;
        [self addSubview:_textView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewContentDidChange:) name:UITextViewTextDidChangeNotification object:_textView];
        _minSize = CGSizeMake(frame.size.width, 50);
        _maxSize = CGSizeMake(frame.size.width, 100);
        _textViewEdgeInsets = UIEdgeInsetsMake(8.0, 0, 8.0, 0);
        _bottomHeight = 0.0;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardFrameWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    }
    return self;
}
-(void)textViewContentDidChange:(NSNotification *)note{
   CGSize contentSize  =   [_textView sizeThatFits:CGSizeMake(self.maxSize.width - self.textViewEdgeInsets.left  - self.textViewEdgeInsets.right, self.maxSize.height - self.textViewEdgeInsets.top  - self.textViewEdgeInsets.bottom)];
    CGFloat height = MIN(contentSize.height, self.maxSize.height - self.textViewEdgeInsets.top  - self.textViewEdgeInsets.bottom);
    height = MAX(self.minSize.height - self.textViewEdgeInsets.top  - self.textViewEdgeInsets.bottom, height);
    
    self.frame = CGRectMake(self.x, APP_HEIGHT - (height + self.textViewEdgeInsets.top  + self.textViewEdgeInsets.bottom)-TopHeight - self.bottomHeight, self.width, height + self.textViewEdgeInsets.top  + self.textViewEdgeInsets.bottom);
    //重点 感谢code4app @sglpublic
    //有个动画效果 在输入很快的时候会有延迟的显示效果
    [self.textView scrollRangeToVisible:self.textView.selectedRange];
    [self layoutIfNeeded];
    
}

-(void)layoutSubviews{
    [super layoutSubviews];
    _textView.frame = CGRectMake(_textViewEdgeInsets.left, _textViewEdgeInsets.top, self.width - _textViewEdgeInsets.left - _textViewEdgeInsets.right, self.height - _textViewEdgeInsets.top - _textViewEdgeInsets.bottom);
}

-(void)keyBoardFrameWillChange:(NSNotification *)note{
    
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    // 取出键盘高度
    CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    CGRect selfCovertframe = [self.superview convertRect:self.frame toView:nil];//后面的View是他的父视图
    //算两者的y差值
    CGFloat offsetY = CGRectGetMaxY(selfCovertframe)  - keyboardF.origin.y;
    self.bottomHeight = CGRectGetHeight(keyboardF);
    if (offsetY > 0) {//交叉了,需要往上移
        [UIView animateWithDuration:duration animations:^{
            self.transform = CGAffineTransformMakeTranslation(0, -offsetY);
        }];
    }else{
        [UIView animateWithDuration:duration animations:^{
            self.transform = CGAffineTransformIdentity;
        }];
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
